package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;
import cn.itcast.bos.web.action.common.BaseAction;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class StandardAction extends BaseAction<Standard>{
	
	
	@Autowired
	private StandardService standardService;
	// 添加操作
		@Action(value = "standard_save", results = { @Result(name = "success",type = "redirect",location = "./pages/base/standard.html") })
		public String save() {
			standardService.save(model);
			System.out.println("添加收派标准....");
			return SUCCESS;
		}
		
		@Action(value = "standard_pageQuery",results = {@Result(name = "success", type = "json")})
		public String pageQuery(){
			Pageable pageable = new PageRequest(page-1,rows);
			Page<Standard> pageData =  standardService.findPageData(pageable);
			
			//返回客户端数据：
			Map<String,Object> result = new HashMap<String,Object>();
			result.put("total", pageData.getTotalElements());
			result.put("rows", pageData.getContent());
			//将map转化为json,发回客户端；
			ActionContext.getContext().getValueStack().push(result);
			return SUCCESS;
		}
		
		@Action(value = "standard_findAll",results = {@Result(name = "success", type = "json")})
		public String findAll(){
			List <Standard> result  = standardService.findAll();
			ActionContext.getContext().getValueStack().push(result);
			return SUCCESS;
		}
}
